package org.vector.eui.demo.ctr;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

public class LoginHandleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //服务器端使用UTF-8编码将响应内容输出到客户端
        response.setCharacterEncoding("UTF-8");
        //通知客户端浏览器以UTF-8编码显示内容，避免产生中文乱码问题
        response.setHeader("content-type", "text/html;charset=UTF-8");
        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");
        Json json = new Json();
        if (userName.equals("gacl") && userPwd.equals("123")) {
            json.setMsg("登录成功");
            json.setSuccess(true);
        }else {
            json.setMsg("用户名或密码错误，登录失败！");
            json.setSuccess(false);
        }
        //使用alibaba(阿里巴巴)的fastJson工具类将Json对象转换成一个json字符串
        String jsonStr = JSON.toJSONString(json);
        //将json字符串作为响应内容输出到客户端浏览器。
        response.getWriter().write(jsonStr);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}